//
//  AppDelegate.swift
//  weibo
//
//  Created by 陈伟 on 15/10/14.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setupAppearance()
        
//        UserAccountViewModel.sharedUserAccount
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //
        window?.rootViewController = CWTabBarController()
        //新特性入口
//        window?.rootViewController  = NewFeatureCollectionViewController()
        //欢迎界面入口
//        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
        //添加通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, queue: nil) { (notification) -> Void in
            
            print(notification.object)
            self.window?.rootViewController = CWTabBarController()
        }
        return true
    }

    private func setupAppearance() {
        UINavigationBar.appearance().tintColor = WBAppearanceTintColor
        UITabBar.appearance().tintColor = WBAppearanceTintColor
    }
}

// MARK: 界面切换
extension AppDelegate  {
    
    private var defaultRootViewController: UIViewController {
        
        if UserAccountViewModel.sharedUserAccount.userLogin {
           return isNewVersion ? NewFeatureCollectionViewController() : WelcomeViewControll()
        }
        
        return CWTabBarController()
    }
    
    /// 判断是否为新版本
    private var isNewVersion: Bool {
        
        let currenVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currenVersion)!
        
        //2.之前的版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandboxVersionKey)
        
        return version > sandboxVersion
    }
    
    
}

